package com.zl.tesseract.scanner.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.zl.tesseract.R;
import com.zl.tesseract.scanner.beans.BrandBean;
import com.zl.tesseract.scanner.beans.JsNativeBean;
import com.zl.tesseract.scanner.utils.DataUtils;
import com.zl.tesseract.scanner.utils.PermissionUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 主页面
 */
public class MainActivity extends Activity implements EasyPermissions.PermissionCallbacks {

    private final String TAG = MainActivity.class.getSimpleName();
    private Context mContext;

    @BindView(R.id.webView)
    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        mContext = this;

        // 首次登录时，需要申请下权限信息
        requestBasicPermission();

        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "执行onResume()方法");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "执行onPause()方法");
    }

    @OnClick({R.id.btn_fc, R.id.btn_move, R.id.btn_out})
    public void onViewClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_fc:
                intent = new Intent(mContext, CollectActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_move:
                intent = new Intent(mContext, MoveActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_out:
                intent = new Intent(mContext, OutActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    /* ********************申请权限-开始************************ */
    private void requestBasicPermission() {
        //检查是否获取该权限
        if (EasyPermissions.hasPermissions(this, PermissionUtils.BASIC_PERMISSIONS)) {
            Log.i("", "已获取权限");
        } else {
            EasyPermissions.requestPermissions(this, "请给予照相机权限,否则app无法正常运行", 1, PermissionUtils.BASIC_PERMISSIONS);
        }
    }

    /**
     * 重写onRequestPermissionsResult，用于接受请求结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //将请求结果传递EasyPermission库处理
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms) && requestCode == 2) {
            PermissionUtils.showDialog(this);
        }
    }
    /* ********************申请权限-结束************************ */

    @SuppressLint({"JavascriptInterface", "AddJavascriptInterface", "SetJavaScriptEnabled"})
    private void initData() {

        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(false);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(false);

        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

        // 设置滚动条不显示
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);

        webView.addJavascriptInterface(this, "jsNative");

        // 设置
        webView.loadUrl("http://47.111.21.41:8080/#/pages/index/index");
    }

    /**
     * js调用原生的方法
     * @param jsonStr json参数
     */
    @JavascriptInterface
    public void startNativeActivity(String jsonStr) {
        Toast.makeText(mContext, "JS调用原生方法", Toast.LENGTH_SHORT).show();
        if (TextUtils.isEmpty(jsonStr)){
            return;
        }
//        JsNativeBean jsNativeBean = DataUtils.getGson().fromJson(jsonStr, new TypeToken<JsNativeBean>() {
//        }.getType());
        Intent intent = new Intent(mContext, EnterActivity.class);
        startActivity(intent);

//        if ("enter".equals(jsNativeBean.getAct_type())) {
//            Intent intent = new Intent(mContext, EnterActivity.class);
//            startActivity(intent);
//        }else if ("collect".equals(jsNativeBean.getAct_type())) {
//            Intent intent = new Intent(mContext, CollectActivity.class);
//            startActivity(intent);
//        }else if ("out".equals(jsNativeBean.getAct_type())) {
//            Intent intent = new Intent(mContext, OutActivity.class);
//            startActivity(intent);
//        }else if ("move".equals(jsNativeBean.getAct_type())) {
//            Intent intent = new Intent(mContext, MoveActivity.class);
//            startActivity(intent);
//        }
    }

}